package com.thenie.controller.admin;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.thenie.pojo.RoomType;
import com.thenie.service.RoomTypeService;
import com.thenie.utils.DataGridViewResult;
import com.thenie.utils.FileNameUtils;
import com.thenie.utils.SystemConstant;
import com.thenie.vo.RoomTypeVo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/admin/roomType")
public class RoomTypeController {
    @Resource
    private RoomTypeService roomTypeService;

    /**
     * 查询所有房型
     * @return
     */
    @RequestMapping("/findAll")
    public String findAll(){
        return JSON.toJSONString(roomTypeService.findRoomTypeList(null));
    }

    /**
     * 返回房型列表
     * @param roomTypeVo
     * @return
     */
    @RequestMapping("/list")
    public DataGridViewResult list(RoomTypeVo roomTypeVo){
        //设置分页
        PageHelper.startPage(roomTypeVo.getPage(), roomTypeVo.getLimit());
        //查询楼层列表
        List<RoomType> roomTypeList = roomTypeService.findRoomTypeList(roomTypeVo);
        //创建分页对象
        PageInfo<RoomType> floorPageInfo = new PageInfo<RoomType>(roomTypeList);

        return new DataGridViewResult(floorPageInfo.getTotal(),floorPageInfo.getList());
    }

    /**
     * 上传文件
     * @param attach
     * @return
     */
    @RequestMapping("/uploadFile")
    public DataGridViewResult uploadFile(MultipartFile attach){
        HashMap<String, Object> dataMap = new HashMap<String, Object>();
        //判断是否选中文件
        if (!attach.isEmpty()) {//选中文件
            //获取源文件的名称
            String oldFileName = attach.getOriginalFilename();
            //获取文件的后缀名
            String extensionName = FileNameUtils.getExtensionName(oldFileName);
            //创建文件的新名称
            String newFileName = UUID.randomUUID().toString().replace("-", "") + "." + extensionName;
            //为了解决同一个文件夹文件过多的问题，根据日期作为文件夹管理
            String yyyyMMdd = new SimpleDateFormat("yyyyMMdd").format(new Date());
            //组装最后的路径文件名
            String pathNewFileName = yyyyMMdd + "/" + newFileName;
            //创建文件对象
            File file = new File("F:/我的项目/hotel_upload/", pathNewFileName);

            //判断是否有文件夹
            if (!file.getParentFile().exists()){//创建文件夹目录
                file.getParentFile().mkdirs();
            }

            try {
                //进行文件上传
                attach.transferTo(file);
                //设置返回消息集合
                dataMap.put("src", "/hotel/show/"+pathNewFileName);
                dataMap.put("imagePath", pathNewFileName);//隐藏域的值
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


        return new DataGridViewResult(dataMap);
    }

    /**
     * 添加一条房型
     * @param roomType
     * @return
     */
    @RequestMapping("/addRoomType")
    public String addRoomType(RoomType roomType){
        HashMap<String, Object> map = new HashMap<String, Object>();
        int i =roomTypeService.addRoomType(roomType);
        if(i>0){//成功
            map.put(SystemConstant.SUCCESS,true);
            map.put(SystemConstant.MESSAGE,"添加成功");
        }else {//失败
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"添加失败");
        }
        return JSON.toJSONString(map);
    }
    /**
     * 修改一条房型信息
     * @param roomType
     * @return
     */
    @RequestMapping("/updateRoomType")
    public String updateRoomType(RoomType roomType) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        int i = roomTypeService.updateRoomType(roomType);
        if (i > 0) {//成功
            map.put(SystemConstant.SUCCESS, true);
            map.put(SystemConstant.MESSAGE, "修改成功");
        } else {//失败
            map.put(SystemConstant.SUCCESS, false);
            map.put(SystemConstant.MESSAGE, "修改失败");
        }
        return JSON.toJSONString(map);
    }

    /**
     * 查询一条房型数据
     * @param roomTypeId
     * @return
     */
    @RequestMapping("/findRoomTypeById")
    public RoomType findRoomTypeById(int roomTypeId){
        return roomTypeService.selectRoomTypeById(roomTypeId);
    }
}
